<?php

namespace app\api\controller;

use addons\third\model\Third;
use alibaba\Check;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Http;
use fast\Random;
use fast\Snowflake;
use think\Config;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Validate;
use wxApp\WxApp;
use function fast\e;

/**
 * 定时任务
 */
class TimedTasks extends Api
{
    protected $noNeedLogin = "*";
    protected $noNeedRight = '*';



    //取消订单
    public function cancel_order() {

        $time = time();

        $detail = Db::name("order")->where("createtime","<=",($time-300))->where("pay_status",0)->select();

        foreach ($detail as $v) {
            Db::startTrans();
            $text = "订单号：".$v["order_no"]."\r\n";
            try {
                //修改车源状态 为在售
                $result = Db::name("car_old")->where("id",$v["car_old_id"])->update([
                    "trade_status"=>1,
                ]);
                if (!$result) {
                    $text .= "车源状态修改失败"."\r\n";
                    Db::rollback();
                }

                $res = Db::name("order")
                    ->where("user_id",$v["user_id"])
                    ->where("status",0)
                    ->where("id",$v["id"])
                    ->update(["status"=>3]);
                if ($res) {
                    Db::commit();

                } else {
                    $text .= "订单状态修改失败"."\r\n";
                    Db::rollback();

                }

            }catch (ValidateException|PDOException|Exception $e) {
                $text .= "错误:".$e->getMessage()."\r\n";
                Db::rollback();

            }
            echo $text;
        }

        echo "取消超时订单成功";
    }

    //车源视频审核
    public function video_check() {

        $detail = Db::name("car_old")->field("id,verify_status,user_id,car_models_name")->where("verify_status","=",0)->select();

        foreach ($detail as $v) {
                $review = Db::name("car_old_video_review")->where("car_old_id",$v["id"])->where("status",0)->select();
                if (count($review) > 0) {
                    $status = false;
                    foreach ($review as $item) {
                        $req = (new Check)->videoModerationResult($item["taskId"]);
                        if ($item["taskId"] == "vi_f_YOONib3ed0tNwPMQ9aYKm8-1AapUQ") {

                        } else {
                            continue;
                        }
                        if ($req["code"] == 200) {
                            if (!isset($req["data"]["frameResult"]["frames"])) {
                                Db::name("car_old_video_review")->where("id",$item["id"])->update(["status"=>1]);
                                continue;
                            }

                            if (count($req["data"]["frameResult"]["frames"]) > 0) {
                                foreach ($req["data"]["frameResult"]["frames"] as $datum) {
                                    //halt($datum["results"]);
                                    if (count($datum["results"]) > 0) {
                                        foreach ($datum["results"] as $frame) {
                                            if (count($frame["result"]) > 0) {
                                                foreach ($frame["result"] as $result) {
                                                    if ($result["confidence"] > 50) {
                                                        $status = true;
                                                        Db::name("car_old_video_review")->where("id",$item["id"])->update(["status"=>2]);
                                                    }
                                                    if ($status) {
                                                        break;
                                                    }
                                                }
                                            }
                                            if ($status) {
                                                break;
                                            }
                                        }
                                    }
                                    if ($status) {
                                        break;
                                    }
                                }


                            } else {
                                Db::name("car_old_video_review")->where("id",$item["id"])->update(["status"=>1]);
                            }
                        }

                        if ($req["code"] != 200 && $req["code"] != 280 && $req["code"] != 288) {
                            Db::name("car_old_video_review")->where("id",$item["id"])->update(["status"=>3]);
                        }

                    }

                    $review = Db::name("car_old_video_review")->where("car_old_id",$v["id"])->where("status","<>",0)->where("status","<>",1)->select();
                    if (count($review) > 0) {
                        Db::name("car_old")->where("id",$v["id"])->update([
                            "verify_status"=>2,
                            "verify_info"=>"视频存在异常或违规，请检查视频或联系客服"
                        ]);

                        $user = Db::name("user")->where("id",$v["user_id"])->find();
                        $data_template = [
                            "thing2" => ["value" => $v["car_models_name"]],
                            "phrase1" => ["value" => "审核被驳回"],
                            "time5" => ["value" => date("Y-m-d H:i:s")]
                        ];

                        (new WxApp)->sendMessage("-bKiYZzxx9pYKrjNDQ3q6a7xnhX7c3rmZ00wxpFL6j0",$user["openid"],$data_template);
                        Db::name("car_old_video_review")->where("car_old_id",$v["id"])->delete();
                    } else {
                        Db::name("car_old")->where("id",$v["id"])->update([
                            "status"=>1,
//                            "verify_status"=>1,
                            "verify_info"=>""
                        ]);


                        $user = Db::name("user")->where("id",$v["user_id"])->find();
                        $data_template = [
                            "thing2" => ["value" => $v["car_models_name"]],
                            "phrase1" => ["value" => "审核通过"],
                            "time5" => ["value" => date("Y-m-d H:i:s")]
                        ];
                        (new WxApp)->sendMessage("-bKiYZzxx9pYKrjNDQ3q6a7xnhX7c3rmZ00wxpFL6j0",$user["openid"],$data_template);
                        Db::name("car_old_video_review")->where("car_old_id",$v["id"])->delete();
                    }

                }

        }

        echo "执行成功";
    }



}


